package com.training.hatton.dropboxclient.content;

import java.util.List;

import org.json.JSONObject;

import android.content.ContentValues;

import com.training.hatton.base.JSONModel;

public class Metadata extends JSONModel {


	private static final String SIZE = "size";
	private static final String BYTES = "bytes";
	private static final String PATH = "path";
	private static final String IS_DIR = "is_dir";
	private static final String REV = "rev";
	private static final String HASH = "hash";
	private static final String MIME_TYPE = "mime_type"; 
	private static final String PARENT = "parent";
	private static final String NAME = "name";
	private static final String IS_DELETED = "is_deleted";
	private static final String JSON = "json";
	private static final String THUMB_EXISTS = "thumb_exists";

	public Metadata(JSONObject object) {
		super(object);
	}

	public Metadata(String source) {
		super(source);
	}

	public String getPath() {
		return getString(PATH);
	}

	public Boolean isDir() {
		return getBoolean(IS_DIR);
	}
	
	public boolean isDeleted() {
		return getBoolean(IS_DELETED);
	}

	public String getSize() {
		return getString(SIZE);
	}

	public String getBytes() {
		return getString(BYTES);
	}

	public String getMimeType() {
		return getString(MIME_TYPE);
	}

	public String getRev() {
		return getString(REV);
	}
	
	public String getHash(){
		return getString(HASH);
	}
	
	public static ContentValues[] listToValues(List<Metadata> list) {
		ContentValues[] values = new ContentValues[list.size()];
		for (int i = 0; i < values.length; i++) {
			Metadata model = list.remove(0);
			values[i] = toValue(model);
		}
		return values;
	}
	public static ContentValues toValue(Metadata md){
		ContentValues value = new ContentValues();	
		value = new ContentValues();
		value.put(PATH, md.getPath());
		value.put(IS_DIR, md.isDir());
		value.put(REV, md.getRev());
		value.put(NAME, md.getName());
		value.put(PARENT, md.getParent());
		value.put(JSON, md.getJsonAsString());
		value.put(THUMB_EXISTS, md.thumbExists());
		value.put(BYTES, md.getBytes());
		return value;
	}

	public boolean thumbExists() {
		return getBoolean(THUMB_EXISTS);
	}

	public String getParent() {
		return createParentByPath();
	}

	private String createParentByPath() {
		String path = getPath();
		String parent;
		if (path.equals("/" + getName())) {
			parent = "/";
		} else {
			parent = path.substring(0, path.lastIndexOf('/'));
		}
		return parent;
	}

	public String getName() {
		return createNameByPath();
	}

	private String createNameByPath() {
		String path = getPath();
		String name = path.substring(path.lastIndexOf('/') + 1, path.length());
		return name;
	}

}
